package com.sanploy.server.controller;

import com.sanploy.server.util.*;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/6/19.
 */
@RestController
public class IndexController {
//    @RequestMapping(value = "hello",method = RequestMethod.GET)
//    private String hello() throws IOException {
////        WechatUtil.getUserOpenid("041rIGBV1XjADS0YLMzV1znrBV1rIGBH");
//        WechatUtil.getWeixingUserInfo("1","2");
//        return "hello";
//    }

    @Autowired
    private WechatUtil wechatUtil;


    @RequestMapping(value = "getUserOpenid",method = RequestMethod.GET)
    private WeixinOauth2Token getUserOpenid(String code) throws IOException {
        return wechatUtil.getUserOpenid(code);
    }

    @RequestMapping(value = "getWeixingUserInfo",method = RequestMethod.GET)
    private WeixinUserInfo getWeixingInfo(String access_token,String openid) throws IOException {
        return wechatUtil.getWeixingUserInfo(access_token,openid);
    }

    @RequestMapping(value = "getSignPackage",method = RequestMethod.GET)
    private JsApi getSignPackage(String pathname) throws IOException {
        System.out.println("Url:"+pathname);
        return wechatUtil.getSignPackage(pathname);
    }

    /**
     * 获取用户是否关注微信公众号
     * @param name
     * @param code
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @RequestMapping(value = "attentionWechat/{name}",method = RequestMethod.GET)
    private void connectWechat(@PathVariable String name, @RequestParam String code,HttpServletResponse resp) throws IOException, InterruptedException {
        resp.sendRedirect(wechatUtil.AttentionWechat(wechatUtil.getUserOpenid(code).getOpenid(),name));
    }

    @RequestMapping(value = "getAttention",method = RequestMethod.GET)
    private void asd(String name,String openid,HttpServletResponse resp) throws IOException, InterruptedException {
        resp.sendRedirect(wechatUtil.AttentionWechat(openid,name));
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    private void asd(HttpServletResponse resp) throws IOException, InterruptedException {
        resp.sendRedirect("/focus.html");
    }

    @ResponseBody
    @RequestMapping(value = "getAccessToken",method = RequestMethod.GET)
    private WechatToken getAccessToken() throws IOException {
        return  wechatUtil.getAccessToken();
    }


}
